package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"shops/cache"
	"shops/models"
)

type ShopsController struct {
	beego.Controller
}
type Msg struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *ShopsController) List() {
	var shop models.Shops
	var shops []*models.Shops
	key := "shops_list"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if cacheData == "" {
		shops, _ = shop.Find()
		jsonData, _ := json.Marshal(shops)
		cache.Rdb.Set(cache.Ctx, key, jsonData, 0)
	} else {
		json.Unmarshal([]byte(cacheData), &shops)
	}
	res := Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: shops,
	}
	c.Data["json"] = res
	c.ServeJSON()
}
